package com.xfly.app.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;

import com.ptmall.app.R;
import com.ptmall.app.base.BaseMvpActivity;
import com.ptmall.app.net.api.ApiDataRepository;
import com.ptmall.app.net.base.ApiNetResponse;
import com.ptmall.app.view.PullToRefreshLayout;
import com.ptmall.app.view.PullableListView;
import com.ptmall.app.view.TitleBarView;
import com.xfly.app.adapter.ZpGoodAdapter;
import com.xfly.app.adapter.ZpSelectGoodAdapter;
import com.xfly.app.bean.KcBean;
import com.xfly.app.bean.ZpBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ZPSelectGoodActivity extends BaseMvpActivity {

    PullToRefreshLayout refresh_view;
    PullableListView listview;
    TitleBarView title_bar;
    ApiDataRepository apiDataRepository = ApiDataRepository.getInstance();

    ZpSelectGoodAdapter kcAdapter;
    RelativeLayout rel;
    List<ZpBean> list = new ArrayList<>();

    int page = 1;
    Button btn;
    String status = "2";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setView(R.layout.activity_lbgood);
    }

    @Override
    public void initViews() {
        title_bar = findViewById(R.id.title_bar);
        rel = findViewById(R.id.rel);
        btn = findViewById(R.id.btn);
        refresh_view = findViewById(R.id.refresh_view);
        listview = findViewById(R.id.listview);
        title_bar.setText("选择链接商品");
        title_bar.setOnTitleBarClickListener(new TitleBarView.TitleBarClickListener() {
            @Override
            public void leftClick() {
                finish();
            }

            @Override
            public void rightClick() {

            }
        });
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                for(ZpBean kcBean:list){
                    if(kcBean.select){
                        Intent intent = new Intent();
                        intent.putExtra("id",kcBean.getId()+"");
//                        intent.putExtra("name",kcBean.name);
//                        intent.putExtra("price",kcBean.price);
//                        intent.putExtra("pic",kcBean.cover);
                        setResult(3,intent);
                        finish();
                    }
                }
            }
        });

        kcAdapter = new ZpSelectGoodAdapter(getContext(),list);
        listview.setAdapter(kcAdapter);

        kcAdapter.setClick(new ZpSelectGoodAdapter.Click() {
            @Override
            public void Click(int position, ZpBean bean) {
                for(ZpBean kcBean:list){
                    kcBean.select = false;
                }
                list.get(position).select = true;
                kcAdapter.pushData(list);
            }

            @Override
            public void Click2(int position) {

            }
        });
//        rel.setVisibility(View.GONE);
        getlist();
    }

    @Override
    public void initEvents() {

        refresh_view.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                page = 1;
                getlist();
            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                page++;
                getlist();
            }
        });
    }

    private void getlist(){
        Map<String,String> map = new HashMap<>();
        map.put("page",page+"");
        map.put("status",status);
        apiDataRepository.getZpList(map, new ApiNetResponse<List<ZpBean>>() {
            @Override
            public void onSuccess(List<ZpBean> data) {
                if(refresh_view!=null){
                    refresh_view.refreshFinish(PullToRefreshLayout.SUCCEED);
                    refresh_view.loadmoreFinish(PullToRefreshLayout.SUCCEED);
                }
                if(page == 1){
                    list.clear();
                }
                list.addAll(data);
                kcAdapter.pushData(list);
            }

            @Override
            public void onFaile(String info, List<ZpBean> data, Throwable throwable) {
                super.onFaile(info, data, throwable);
                if(refresh_view!=null){
                    refresh_view.refreshFinish(PullToRefreshLayout.SUCCEED);
                    refresh_view.loadmoreFinish(PullToRefreshLayout.SUCCEED);
                    list.clear();
                    kcAdapter.pushData(list);
                }
            }
        });
    }
}
